/*
*    UNIVERSIDADE DE SÃO PAULO.
*    Author: Marco Aurélio Gerosa (gerosa@ime.usp.br)
*
*    This file is part of Groupware Workbench (http://www.groupwareworkbench.org.br).
*
*    Groupware Workbench is free software: you can redistribute it and/or modify
*    it under the terms of the GNU Lesser General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    Groupware Workbench is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Lesser General Public License for more details.
*
*    You should have received a copy of the GNU Lesser General Public License
*    along with Swift.  If not, see <http://www.gnu.org/licenses/>.
*/
package br.org.groupwareworkbench.aun.news;

import java.util.List;

import br.com.caelum.vraptor.Delete;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.ioc.RequestScoped;
import br.com.caelum.vraptor.view.Results;
import br.org.groupwareworkbench.collablet.coop.recommend.RecommendMgrInstance;
import br.org.groupwareworkbench.core.framework.Collablet;
import br.org.groupwareworkbench.core.framework.WidgetInfo;

@RequestScoped
@Resource
public class NewsMgrController {

    private final Result result;
    private final WidgetInfo info;

    public NewsMgrController(Result result, WidgetInfo info) {
        this.result = result;
        this.info = info;
    }

    @Get
    @Path(value = "/groupware-workbench/news/{newsMgr}/list")
    public List<News> list(NewsMgrInstance newsMgr) {
        addIncludes(newsMgr);
        return newsMgr.list();
    }

    @Get
    @Path(value = "/groupware-workbench/news/{id}/show")
    public void show(final long id) {
        retrieve(id);
    }

    @Get
    @Path(value="/groupware-workbench/news/{newsMgr}/create")
    public void create(NewsMgrInstance newsMgr) {
        News news = new News();
        addIncludes(newsMgr);
        result.include("news", news);
        result.use(Results.representation()).from(news).serialize();
    }

    @Get
    @Path(value = "/groupware-workbench/news/{id}")
    public void retrieve(long id) {
        News news = News.findById(id);
        if (news == null) {
            result.notFound();
            return;
        }

        NewsMgrInstance newsMgr = (NewsMgrInstance) news.getCollablet().getBusinessObject();
        addIncludes(newsMgr);
        result.include("news", news);
        result.use(Results.representation()).from(news).serialize();
    }

    @Post
    @Path(value = "/groupware-workbench/news/{newsMgr}")
    public void save(NewsMgrInstance newsMgr, final News news) {
        newsMgr.save(news);

        Collablet collablet = newsMgr.getCollablet();
        collablet.processWidgets(info, news);

        RecommendMgrInstance recommendMgr =
                (RecommendMgrInstance) collablet.getDependency("recommendMgr").getBusinessObject();

        recommendMgr.generateRelated(newsMgr.list(), news);

        result.use(Results.logic()).redirectTo(NewsMgrController.class).list(newsMgr);
    }

    @Post
    @Path(value = "/groupware-workbench/news/{id}/updateRelated")
    public void updateRelated(long id) {
        News news = News.findById(id);
        if (news == null) {
            result.notFound();
            return;
        }

        Collablet collablet = news.getCollablet();
        NewsMgrInstance newsMgr = (NewsMgrInstance) collablet.getBusinessObject();
        collablet.processWidgets(info, news);

        /*RecommendMgrInstance recommendMgr =
                (RecommendMgrInstance) collablet.getDependency("recommendMgr").getBusinessObject();

        recommendMgr.generateRelated(newsMgr.list(), news);*/

        result.use(Results.logic()).redirectTo(NewsMgrController.class).list(newsMgr);
    }

    @Delete
    @Path(value = "/groupware-workbench/news/{id}")
    public void remove(long id) {
        News news = News.findById(id);
        if (news == null) {
            result.notFound();
            return;
        }

        NewsMgrInstance newsMgr = (NewsMgrInstance) news.getCollablet().getBusinessObject();
        news.delete();
        result.use(Results.logic()).redirectTo(NewsMgrController.class).list(newsMgr);
    }

//    @Get
//    @Path(value = "/groupware-workbench/news/{newsMgr}/configure")
//    public void listNewsConfigure(NewsMgrInstance newsMgr) {
//        // FIXME: Deveria acontecer alguma coisa em este método.
//        addIncludes(newsMgr);
//    }

    @Get
    @Path(value = "/groupware-workbench/news/{newsMgr}/copyNewsFromAUN")
    public void copyFromAUN(NewsMgrInstance newsMgr) {
        newsMgr.copyFromAUN();
        result.use(Results.logic()).redirectTo(NewsMgrController.class).list(newsMgr);
    }

    private void addIncludes(NewsMgrInstance newsMgr) {
        result.include("newsMgr", newsMgr);
        newsMgr.getCollablet().includeDependencies(result);
    }
}